package com.wyg.epuser.config.apo;

import com.wyg.epcommonbase.common.annotation.AuthRoles;
import com.wyg.epcommonbase.common.util.AssertUtil;
import com.wyg.epuser.utils.RequestUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @Author: WangYongGan
 * @Date: 2022/5/11 22:25
 * @Description:
 */
@Component
@Aspect
public class AuthPermAop {
    @Pointcut("@annotation(com.wyg.epcommonbase.common.annotation.AuthPerms)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        AssertUtil.isTrue(!RequestUtil.logined(), 108, "请登录");
        final AuthRoles annotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(AuthRoles.class);
        final String[] needRoles = annotation.value().split("&");
        final Set<String> roles = RequestUtil.getPerms();
        for (String needRole : needRoles) {
            AssertUtil.isTrue(!roles.contains(needRole), 108, "没有权限");
        }
    }
}
